<template>
  <div class="main-content">
    <!-- 列表页 -->
    <div v-if="showFlag">
      <el-form
          :inline="true"
          :model="searchForm"
          class="search-form"
      >
        <el-row
            :gutter="20"
            class="slt"
        >
          <el-form-item label="食谱名称">
            <el-input
                prefix-icon="el-icon-search"
                v-model="searchForm.shipumingcheng"
                placeholder="食谱名称"
                clearable
            ></el-input>
          </el-form-item>
          <el-form-item class="select" label="体质类型" prop="tizhileixing">
            <el-select
                @change="tizhileixingChange"
                clearable
                v-model="searchForm.tizhileixing"
                placeholder="请选择体质类型"
            >
              <el-option
                  v-for="(item, index) in tizhileixingOptions"
                  v-bind:key="index"
                  :label="item"
                  :value="item"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item class="select" label="是否付费" prop="ispay">
            <el-select
              clearable
              v-model="searchForm.ispay"
              placeholder="请选择是否付费"
            >
              <el-option
                :label="'是'"
                :value="'1'"
              >
              </el-option>
              <el-option
                :label="'否'"
                :value="'0'"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button
                icon="el-icon-search"
                type="success"
                @click="search()"
            >查询
            </el-button>
          </el-form-item>
        </el-row>

        <el-row class="ad">
          <el-form-item>
            <el-button
                type="success"
                icon="el-icon-plus"
                @click="addOrUpdateHandler()"
            >新增
            </el-button>
            <el-button
                :disabled="dataListSelections.length <= 0"
                type="danger"
                icon="el-icon-delete"
                @click="deleteHandler()"
            >删除
            </el-button>
          </el-form-item>
        </el-row>
      </el-form>

      <div class="table-content">
        <el-table
            class="tables"
            :size="'medium'"
            :show-header="true"
            :border="true"
            :fit="true"
            :stripe="true"
            :style="{
            width: '100%',
            fontSize: '14px',
            color: '#606266',
          }"
            v-if="true"
            :data="dataList"
            v-loading="dataListLoading"
            highlight-current-row
            @selection-change="selectionChangeHandler"
        >
          <el-table-column
              type="selection"
              align="center"
              width="50"
          >
          </el-table-column>
          <el-table-column
              label="索引"
              align="center"
              type="index"
              width="50"
          />
          <el-table-column
              align="center"
              prop="shipumingcheng"
              header-align="center"
              label="食谱名称"
          >
            <template slot-scope="scope">
              {{ scope.row.shipumingcheng }}
            </template>
          </el-table-column>
          <el-table-column
              align="center"
              prop="tizhileixing"
              header-align="center"
              label="体质类型"
          >
            <template slot-scope="scope">
              {{ scope.row.tizhileixing }}
            </template>
          </el-table-column>
          <el-table-column
              align="center"
              prop="ispay"
              header-align="center"
              label="是否付费"
          >
            <template slot-scope="scope">
              {{ scope.row.ispay == 1 ? '是' : '否' }}
            </template>
          </el-table-column>
          <el-table-column
              align="center"
              prop="price"
              header-align="center"
              label="价格"
          >
            <template slot-scope="scope">
              {{ scope.row.ispay == 1 ? scope.row.price + '元' : '免费' }}
            </template>
          </el-table-column>
          <el-table-column
              align="center"
              prop="shipufengmian"
              header-align="center"
              width="200"
              label="食谱封面"
          >
            <template slot-scope="scope">
              <div v-if="scope.row.shipufengmian" class="table-image">
                <img
                    :src="$base.url + scope.row.shipufengmian.split(',')[0]"
                    class="table-img"
                />
              </div>
              <div v-else>无图片</div>
            </template>
          </el-table-column>
          <el-table-column
              width="360"
              align="center"
              header-align="center"
              label="操作"
              fixed="right"
              class-name="operation-column"
          >
            <template slot-scope="scope">
              <div class="operation-buttons">
                <el-button
                    type="success"
                    icon="el-icon-tickets"
                    size="mini"
                    @click="addOrUpdateHandler(scope.row.id, 'info')"
                >详情
                </el-button>
                <el-button
                    type="primary"
                    icon="el-icon-edit"
                    size="mini"
                    @click="addOrUpdateHandler(scope.row.id)"
                >修改
                </el-button>
                <el-button
                    type="primary"
                    icon="el-icon-chat-dot-round"
                    size="mini"
                    @click="disscussListHandler(scope.row.id)"
                >查看评论
                </el-button>
                <el-button
                    type="danger"
                    icon="el-icon-delete"
                    size="mini"
                    @click="deleteHandler(scope.row.id)"
                >删除
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
            clsss="pages"
            :layout="layouts"
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :current-page="pageIndex"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="10"
            :total="totalPage"
            class="pagination-content"
        ></el-pagination>
      </div>
    </div>
    <!-- 添加/修改页面  将父组件的search方法传递给子组件-->
    <add-or-update
        v-if="addOrUpdateFlag"
        :parent="this"
        ref="addOrUpdate"
    ></add-or-update>
  </div>
</template>
<script>
import axios from 'axios'
import AddOrUpdate from "./add-or-update";

export default {
  data() {
    return {
      searchForm: {
        key: "",
        ispay: "",
      },
      form: {},
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      showFlag: true,
      sfshVisiable: false,
      shForm: {},
      chartVisiable: false,
      addOrUpdateFlag: false,
      layouts: 'total, sizes, prev, pager, next, jumper',
      tizhileixingOptions: [],
    };
  },
  created() {
    this.getDataList();
    this.init();
  },
  mounted() {

  },
  filters: {
    htmlfilter: function (val) {
      return val.replace(/<[^>]*>/g).replace(/undefined/g, '');
    }
  },
  components: {
    AddOrUpdate,
  },
  methods: {
    init() {
      this.$http({
        url: "option/tizhileixing/tizhileixing",
        method: "get"
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.tizhileixingOptions = data.data;
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    tizhileixingChange(val) {
      this.searchForm.tizhileixing = val;
    },
    // 搜索
    search() {
      this.pageIndex = 1;
      this.getDataList();
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      let params = {
        page: this.pageIndex,
        limit: this.pageSize,
        sort: 'id',
      }
      if (this.searchForm.shipumingcheng !== '' && this.searchForm.shipumingcheng !== undefined) {
        params['shipumingcheng'] = '%' + this.searchForm.shipumingcheng + '%'
      }
      if (this.searchForm.tizhileixing !== '' && this.searchForm.tizhileixing !== undefined) {
        params['tizhileixing'] = this.searchForm.tizhileixing
      }
      if (this.searchForm.ispay !== '' && this.searchForm.ispay !== undefined) {
        params['ispay'] = this.searchForm.ispay
      }
      this.$http({
        url: "tizhishipu/page",
        method: "get",
        params: params
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.dataList = data.data.list;
          this.totalPage = data.data.total;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandler(val) {
      this.dataListSelections = val;
    },
    // 添加/修改
    addOrUpdateHandler(id, type) {
      this.showFlag = false;
      this.addOrUpdateFlag = true;
      this.crossAddOrUpdateFlag = false;
      if (type !== 'info') {
        type = 'else';
      }
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id, type);
      });
    },
    // 查看评论
    disscussListHandler(id, type) {
      this.$router.push({path: '/discusstizhishipu', query: {refid: id}});
    },
    // 下载
    download(file) {
      window.open(`${file}`)
    },
    // 删除
    deleteHandler(id) {
      var ids = id
          ? [Number(id)]
          : this.dataListSelections.map(item => {
            return Number(item.id);
          });
      this.$confirm(`确定进行[${id ? "删除" : "批量删除"}]操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$http({
          url: "tizhishipu/delete",
          method: "post",
          data: ids
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.search();
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    },


  }

};
</script>
<style lang="scss" scoped>
.main-content {
  .slt {
    margin: 0 0 20px 0;
    display: flex;
    justify-content: flex-start;

    .el-form-item {
      margin-right: 15px;
      margin-bottom: 0;
    }
  }

  .ad {
    margin-bottom: 20px;
  }

  .el-button {
    margin-right: 8px;
  }

  .el-table {
    border-radius: 8px;
    overflow: hidden;

    .el-button {
      padding: 6px 10px;
      font-size: 12px;
      margin: 3px;
      min-width: 70px;
      border-radius: 3px;

      &.el-button--success {
        background-color: #4a90e2;
        border-color: #4a90e2;
      }

      &.el-button--primary {
        background-color: #4a90e2;
        border-color: #4a90e2;
      }

      &.el-button--danger {
        background-color: #ff3b30;
        border-color: #ff3b30;
      }

      i {
        margin-right: 3px;
      }
    }

    .el-table-column--operation {
      .cell {
        display: flex;
        flex-wrap: wrap;
        justify-content: center;
      }
    }
  }

  .pagination-content {
    display: flex;
    justify-content: center;
    margin-top: 20px;
  }
}

.table-content {
  margin-top: 20px;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
  background-color: #ffffff;
}

.tables {
  width: 100%;
}

.tables .el-table__header th {
  background-color: #f5f7fa !important;
  color: #606266;
  font-weight: bold;
  border-color: #ebeef5;
}

.tables .el-table__row {
  background-color: #ffffff !important;
}

.tables .el-table__row:hover {
  background-color: #f5f7fa !important;
}

.tables .el-table__row td {
  padding: 10px 0;
  color: #606266 !important;
}

.el-pagination {
  margin-top: 20px;
  text-align: center;
}

.search-form {
  margin-bottom: 20px;
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

.search-form .el-form-item {
  margin-bottom: 10px;
}

.search-form .el-input__inner {
  border-radius: 4px;
  border-color: #dcdfe6;
}

.search-form .el-input__inner:hover {
  border-color: #c0c4cc;
}

.search-form .el-input__inner:focus {
  border-color: #4a90e2;
}

.search-form .el-button {
  border-radius: 4px;
  padding: 10px 20px;
  transition: all 0.3s;
}

.search-form .el-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.table-image {
  display: flex;
  justify-content: center;
  align-items: center;
}

.table-img {
  width: 100px;
  height: 100px;
  object-fit: cover;
  border-radius: 4px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
}

.table-img:hover {
  transform: scale(1.05);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

/* 操作按钮样式优化 */
.operation-buttons {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 5px;
  padding: 5px 0;
}

.operation-buttons .el-button {
  margin: 3px;
  flex-grow: 0;
  min-width: 74px;
  font-size: 12px;
  height: 32px;
  padding: 0 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  transition: all 0.25s ease;
}

.operation-buttons .el-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.operation-buttons .el-button--success {
  background-color: #67c23a;
  border-color: #67c23a;
}

.operation-buttons .el-button--primary {
  background-color: #409eff;
  border-color: #409eff;
}

.operation-buttons .el-button--danger {
  background-color: #f56c6c;
  border-color: #f56c6c;
}

/* 当表格列宽度不够时，确保操作按钮区域仍然美观 */
.operation-column {
  .cell {
    padding: 5px !important;
    overflow: visible !important;
  }
}

/* 覆盖默认的表格背景色 */
/deep/ .el-table,
/deep/ .el-table tr,
/deep/ .el-table__body {
  background-color: #ffffff !important;
}

/deep/ .el-table th {
  background-color: #f5f7fa !important;
}

/deep/ .el-table--striped .el-table__body tr.el-table__row--striped td {
  background-color: #fafafa;
}

/* 修改表头和整体表格样式 */
/deep/ .el-table th.is-leaf {
  border-bottom: 1px solid #ebeef5;
}

/deep/ .el-table__fixed-right::before,
/deep/ .el-table__fixed::before {
  background-color: #ebeef5;
}

.main-content {
  background-color: #f5f7fa;
  padding: 20px;
  min-height: 100%;
}
</style>
